package leetcode.D200.T111;

import util.TreeNode;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @File Info: leetcode -- <Solution>
 * @Author: 18362
 * @Create: 2022-02-26 17:40:24 星期六
 */
class Solution {
    public int minDepth(TreeNode root) {
        if (root == null)
            return 0;
        int level = 1, minLevel=Integer.MAX_VALUE;
        Queue<TreeNode> dq = new LinkedList<>();
        dq.add(root);
        while (!dq.isEmpty()) {
            int size = dq.size();
            for (int i = 0; i < size; i++) {
                TreeNode front = dq.poll();
                if (front.left == null && front.right == null)
                    minLevel = Math.min(minLevel, level);
                if (front.left != null)
                    dq.add(front.left);
                if (front.right != null)
                    dq.add(front.right);
            }
            level++;
        }
        return minLevel;
    }
}
